package com.zjy.basic.listener;

import com.zjy.basic.service.PermissionScanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener//申明自定义的web监听器，被容器注册和使用
@Slf4j
public class PermissionScanInitListener implements ServletContextListener {

    @Autowired
    private PermissionScanService permissionScanService;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        log.info("我启动了");
        //异步线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("开始保存permission");
                permissionScanService.scan();
                log.info("permission保存完毕");
            }
        }).start();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}